/**
 * Yolema.com Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */
package org.grayrabbit.pay.core.action.system;

import net.sf.json.JSONObject;
import org.grayrabbit.web.struts2.GenericAction;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.grayrabbit.pay.chinapay.config.ChinaPayInfo;
import org.grayrabbit.pay.core.model.results.GenericsResult;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.convention.annotation.Result;
import org.grayrabbit.pay.core.model.results.PayResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.grayrabbit.pay.core.model.pages.PageList;
import org.grayrabbit.pay.core.model.entity.system.TradeTerminal;
import org.grayrabbit.pay.core.biz.manager.system.TradeTerminalManager;

/**
 * 
 * 
 * @author liyd
 * @version $Id: TradeTerminalAction.java, v 0.1 Sat Jun 02 22:00:25 CST 2012 liyd Exp $
 */
@Results({
          @Result(name = GenericAction.RELOAD, location = "trade_terminal_list", type = "redirectAction"),
          @Result(name = GenericAction.INPUT, location = "trade_terminal-input.vm"),
          @Result(name = GenericAction.LIST, location = "trade_terminal-list.vm"),
          @Result(name = "terminalConfig", location = "terminal_config-input.vm") })
@InterceptorRefs({ @InterceptorRef(value = "token", params = { "includeMethods", "save" }),
                  @InterceptorRef("login"), @InterceptorRef("defaultStack") })
public class TradeTerminalAction extends GenericAction {

    /** serialVersionUID */
    private static final long       serialVersionUID = 8148345003602233938L;

    /** 业务对象manager接口 */
    @Autowired
    private TradeTerminalManager    tradeTerminalManager;

    /** 业务实体对象 */
    private TradeTerminal           tradeTerminal    = new TradeTerminal();
    private ChinaPayInfo            chinaPayInfo     = null;
    /** 业务实体对象列表 */
    private PageList<TradeTerminal> tradeTerminalList;

    /**
     * 查询列表
     * 访问URL /trade_terminal_list.action
     * 
     * @return
     */
    @Action(value = "trade_terminal_list")
    public String list() {

        GenericsResult<PageList<TradeTerminal>> result = tradeTerminalManager
            .queryPageList(tradeTerminal);
        if (!result.isSuccess()) {
            this.addActionError(result.getResultMsg());
        }
        tradeTerminalList = result.getResultData();
        return LIST;
    }

    public ChinaPayInfo getChinaPayInfo() {
        return chinaPayInfo;
    }

    public void setChinaPayInfo(ChinaPayInfo chinaPayInfo) {
        this.chinaPayInfo = chinaPayInfo;
    }

    /**
     * 跳转到添加修改页面
     * 访问URL /trade_terminal_input.action
     * 
     * @return
     */
    @Action(value = "trade_terminal_input")
    public String input() {

        if (tradeTerminal.getTerminalId() == null) {
            return INPUT;
        }

        GenericsResult<TradeTerminal> result = tradeTerminalManager.getById(tradeTerminal
            .getTerminalId());

        if (!result.isSuccess()) {
            this.addActionError(result.getResultMsg());
        }
        tradeTerminal = result.getResultData();

        return INPUT;
    }

    /**
     * 启用 禁用终端
     * 访问URL /trade_terminal_changeStatus.action
     * @return
     */
    @Action(value = "trade_terminal_changeStatus")
    public String changeStatus() {

        PayResult result = tradeTerminalManager.saveStatus(tradeTerminal);
        JSONObject jsonObject = JSONObject.fromObject(result);
        return renderJson(jsonObject.toString());
    }

    /**
     * 保存
     * 访问URL /trade_terminal_save.action
     * 
     * @return
     */
    @Action(value = "trade_terminal_save")
    public String save() {

        PayResult result = null;

        if (tradeTerminal.getTerminalId() != null) {
            result = tradeTerminalManager.update(tradeTerminal);
        } else {
            result = tradeTerminalManager.insert(tradeTerminal);
        }

        if (!result.isSuccess()) {
            this.addActionError(result.getResultMsg());
            return INPUT;
        }
        return RELOAD;
    }

    /**
     * 判断该code是否已存在
     * 访问URL /app_system_appCode.action
     * @return
     */
    @Action(value = "trade_terminal_terminalCode")
    public String checkRepeatCode() {
        PayResult result = tradeTerminalManager.getIfRepeatByCode(tradeTerminal);
        String resultMsg = result.isSuccess() + "";
        return renderText(resultMsg);
    }

    /**
     * 删除
     * 访问URL /trade_terminal_delete.action
     * 
     * @return
     */
    @Action(value = "trade_terminal_delete")
    public String delete() {

        PayResult result = tradeTerminalManager.deleteBatchById(key);
        JSONObject jsonObject = JSONObject.fromObject(result);

        return renderJson(jsonObject.toString());
    }

    /**
     * Getter method for property <tt>tradeTerminal</tt>.
     * 
     * @return property value of tradeTerminal
     */
    public TradeTerminal getTradeTerminal() {
        return tradeTerminal;
    }

    /**
     * Setter method for property <tt>tradeTerminal</tt>.
     * 
     * @param tradeTerminal value to be assigned to property tradeTerminal
     */
    public void setTradeTerminal(TradeTerminal tradeTerminal) {
        this.tradeTerminal = tradeTerminal;
    }

    /**
     * Getter method for property <tt>tradeTerminalList</tt>.
     * 
     * @return property value of tradeTerminalList
     */
    public PageList<TradeTerminal> getTradeTerminalList() {
        return tradeTerminalList;
    }

    /**
     * Setter method for property <tt>tradeTerminalList</tt>.
     * 
     * @param districtList value to be assigned to property tradeTerminalList
     */
    public void setTradeTerminalList(PageList<TradeTerminal> tradeTerminalList) {
        this.tradeTerminalList = tradeTerminalList;
    }

}
